package com.selfmade.transferutil.ui;

/**
 * Created by Administrator on 2016/8/18.
 */

import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.ListFragment;
import android.text.format.Time;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.selfmade.transferutil.R;
import com.selfmade.transferutil.app.MyApp;
import com.selfmade.transferutil.model.Data;

import java.util.ArrayList;

public class DataListFragment extends ListFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DataAdapter adapter = new DataAdapter(MyApp.transition.queryHistory());
        setListAdapter(adapter);
    }

    //内部类
    private class DataAdapter extends ArrayAdapter<Data> {
        public DataAdapter(ArrayList<Data> datas) {
            super(getActivity(), 0, datas);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // 检查传入视图对象是否是复用对象，否则从定制布局里产生一个新的视图对象
            if (null == convertView) {
                convertView = getActivity().getLayoutInflater().inflate(R.layout.list_data, null);
            }
            Data d = getItem(position);
            //获取对象后，引用视图对象中的各个组件，以Data的数据信息对应配置视图对象，最后把视图对象返回给ListView
            TextView receiver = (TextView)convertView.findViewById(R.id.data_list_receiver);
            receiver.setText(d.getReceiver());
            TextView transfer = (TextView)convertView.findViewById(R.id.data_list_transfer);
            transfer.setText(d.getTransfer());
            TextView time = (TextView)convertView.findViewById(R.id.data_list_time);
            time.setText(timeStampToDate(d.getTime()));
            TextView money = (TextView) convertView.findViewById(R.id.data_list_money);
            money.setText(Integer.toString(d.getMoney()));
            return convertView;
        }
    }

    @Override
    public void onResume(){
        super.onResume();
        //((DataAdapter)getListAdapter()).notifyDataSetChanged();
    }

    public static String timeStampToDate(String timeStampString) {
        Long timeStamp = Long.parseLong(timeStampString);
        String date = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date(timeStamp));
        return date;
    }
}



